from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import pandas as pd


class QutoEs:
    def __init__(self):
        self.url='https://quotes.toscrape.com/login'
        self.username = '13471479481'
        self.password = 'moqiaoli123926'


    def open_html(self):
        chrome_options = Options()
        chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")  # 前面设置的端口号
        self.driver=webdriver.Chrome(options=chrome_options)
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.find_element(by=By.ID,value='username').send_keys(self.username)
        self.driver.find_element(by=By.ID,value='password').send_keys(self.password)
        self.driver.find_element(by=By.CSS_SELECTOR,value='body > div > form > input.btn.btn-primary').click()
        #点击右侧选择任意一个标签
        self.driver.find_element(by=By.XPATH,value='/html/body/div/div[2]/div[2]/span[1]/a').click()
    #获取数据

    def have_data(self):
        # 获取内容、作者、以及tag
        all_tag=[]

        content=[i.text for i in self.driver.find_elements(by=By.XPATH,value="//span[@class='text']")]
        author=[i.text for i in self.driver.find_elements(by=By.XPATH,value='//small[@class="author"]')]
        for i in range(len(author)):
            tag=[ k.text for k in self.driver.find_elements(by=By.XPATH,value='/html/body/div/div[2]/div[1]/div[%s]/div/a'%str(i+1))]
            all_tag.append(','.join(tag))
        data=pd.DataFrame()
        data['author']=author
        data['content']=content
        data['tags']=all_tag
        data.to_csv('data.csv',encoding='utf-8-sig')






if __name__=='__main__':
    mql=QutoEs()
    mql.open_html()
    mql.have_data()








